/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.emergency.command.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.bizz.emergency.command.entity.ErAssessKpiEntity;
import com.hopes.bizz.emergency.command.service.ErAssessKpiService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 应急事件评估指标
 *
 * @author cx
 * @date 2024-01-23 09:53:05
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/command/assessKpi" )
@Tag(description = "assessKpi" , name = "应急事件评估指标管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ErAssessKpiController {

    private final  ErAssessKpiService erAssessKpiService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param erAssessKpi 应急事件评估指标
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('command_assessKpi_view')" )
    public R geterAssessKpiPage(Page page, ErAssessKpiEntity erAssessKpi) {
        LambdaQueryWrapper<ErAssessKpiEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.like(StrUtil.isNotBlank(erAssessKpi.getAssessItemName()),ErAssessKpiEntity::getAssessItemName,erAssessKpi.getAssessItemName());
		wrapper.eq(StrUtil.isNotBlank(erAssessKpi.getKpiModule()),ErAssessKpiEntity::getKpiModule,erAssessKpi.getKpiModule());
        return R.ok(erAssessKpiService.page(page, wrapper));
    }

    /**
     * 查询list
     * @param erAssessKpi 应急事件评估指标
     * @return
     */
    @Operation(summary = "查询list" , description = "查询list" )
    @GetMapping("/list" )
    @PreAuthorize("@pms.hasPermission('command_assessKpi_view')" )
    public R geterAssessKpiList( ErAssessKpiEntity erAssessKpi) {
        LambdaQueryWrapper<ErAssessKpiEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(erAssessKpi.getKpiModule()),ErAssessKpiEntity::getKpiModule,erAssessKpi.getKpiModule());
        return R.ok(erAssessKpiService.list(wrapper));
    }

    /**
     * 通过id查询应急事件评估指标
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('command_assessKpi_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(erAssessKpiService.getById(id));
    }

    /**
     * 新增应急事件评估指标
     * @param erAssessKpi 应急事件评估指标
     * @return R
     */
    @Operation(summary = "新增应急事件评估指标" , description = "新增应急事件评估指标" )
    @SysLog("新增应急事件评估指标" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('command_assessKpi_add')" )
    public R save(@RequestBody ErAssessKpiEntity erAssessKpi) {
        return R.ok(erAssessKpiService.save(erAssessKpi));
    }

    /**
     * 修改应急事件评估指标
     * @param erAssessKpi 应急事件评估指标
     * @return R
     */
    @Operation(summary = "修改应急事件评估指标" , description = "修改应急事件评估指标" )
    @SysLog("修改应急事件评估指标" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('command_assessKpi_edit')" )
    public R updateById(@RequestBody ErAssessKpiEntity erAssessKpi) {
        return R.ok(erAssessKpiService.updateById(erAssessKpi));
    }

    /**
     * 通过id删除应急事件评估指标
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除应急事件评估指标" , description = "通过id删除应急事件评估指标" )
    @SysLog("通过id删除应急事件评估指标" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('command_assessKpi_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(erAssessKpiService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param erAssessKpi 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('command_assessKpi_export')" )
    public List<ErAssessKpiEntity> export(ErAssessKpiEntity erAssessKpi) {
        return erAssessKpiService.list(Wrappers.query(erAssessKpi));
    }
}